#!/usr/bin/env bash
set -eux

. $(dirname "$0")/../common.sh

RENAISSANCE_OPTS="${RENAISSANCE_OPTS:--r 3 --no-forced-gc}"
PREFIX="${PREFIX:-r3ngc_6g_je-pt-numa}"

#RENAISSANCE_OPTS="${RENAISSANCE_OPTS:--r 3}"
#PREFIX="${PREFIX:-r3_6g_je-pt-numa}"

# database: neo4j-analytics can't run on JDK 8 and 17
# database: db-shootout can't run on JDK 17
BENCHMARKS='apache-spark concurrency functional scala web'

#######################################################################

run() {
	local REP=$1
	local JAVA=$2
	local GC_OPTS=$3
	local OUT_FN_PREFIX=${PREFIX}_${REP}_$4
	rm -rf harness* launcher*
	ldd $JAVA > $OUT_FN_PREFIX.ldd
	/usr/bin/time -v -o $OUT_FN_PREFIX.time \
		$JAVA -XX:+PrintFlagsFinal \
		-XX:+UnlockExperimentalVMOptions \
		-XX:+PrintGCDetails -Xloggc:$OUT_FN_PREFIX.gc \
		-Xms$HEAP_SIZE -Xmx$HEAP_SIZE \
		$JAVA_OPTS $GC_OPTS \
		-jar renaissance-gpl-0.13.0.jar \
		$RENAISSANCE_OPTS \
		--json $OUT_FN_PREFIX.json \
		$BENCHMARKS >$OUT_FN_PREFIX.out 2>&1
}

#######################################################################

for (( rep=0; rep<$REPS; rep++ )); do

	run	$rep	$TEMURIN8	-XX:+UseParallelGC	temurin8-parallel
	run	$rep	$TEMURIN8	-XX:+UseConcMarkSweepGC	temurin8-cms
	run	$rep	$TEMURIN8	-XX:+UseG1GC		temurin8-g1

	run	$rep	$TEMURIN11	-XX:+UseParallelGC	temurin11-parallel
	run	$rep	$TEMURIN11	-XX:+UseConcMarkSweepGC	temurin11-cms
	run	$rep	$TEMURIN11	-XX:+UseG1GC		temurin11-g1
	run	$rep	$TEMURIN11	-XX:+UseShenandoahGC	temurin11-shenandoah

	run	$rep	$KONA11		-XX:+UseParallelGC	kona11-parallel
	run	$rep	$KONA11		-XX:+UseConcMarkSweepGC	kona11-cms
	run	$rep	$KONA11		-XX:+UseG1GC		kona11-g1
	run	$rep	$KONA11		-XX:+UseZGC		kona11-z

	run	$rep	$DRAGONWELL11	-XX:+UseParallelGC	dragonwell11-parallel
	run	$rep	$DRAGONWELL11	-XX:+UseConcMarkSweepGC	dragonwell11-cms
	run	$rep	$DRAGONWELL11	-XX:+UseG1GC		dragonwell11-g1
	run	$rep	$DRAGONWELL11	-XX:+UseZGC		dragonwell11-z

	run	$rep	$JDK17		-XX:+UseParallelGC	jdk17-parallel
	run	$rep	$JDK17		-XX:+UseG1GC		jdk17-g1
	run	$rep	$JDK17		-XX:+UseZGC		jdk17-z

	run	$rep	$TEMURIN17	-XX:+UseParallelGC	temurin17-parallel
	run	$rep	$TEMURIN17	-XX:+UseG1GC		temurin17-g1
	run	$rep	$TEMURIN17	-XX:+UseZGC		temurin17-z
	run	$rep	$TEMURIN17	-XX:+UseShenandoahGC	temurin17-shenandoah

	run	$rep	$SEMERU11	-Xgcpolicy:gencon	semeru11-gencon
	run	$rep	$SEMERU11	-Xgcpolicy:balanced	semeru11-balanced
	run	$rep	$SEMERU11	-Xgcpolicy:optthruput	semeru11-optthruput

	run	$rep	$ZING11		' '			zing11

done
